package com.meida.module.bus.provider.handler;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.google.common.collect.Maps;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.interceptor.SaveInterceptor;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.bus.client.entity.*;
import com.meida.module.bus.provider.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Component("materialPlan100SaveHandler")
public class MaterialPlan100SaveHandler implements SaveInterceptor<BusMaterial>{
    @Autowired
    private BusMaterialPlan100Service busMaterialPlan100Service;
    @Autowired
    private BusVersionService busVersionService;
    @Autowired
    private BusProjectService busProjectService;
    @Autowired
    private BusMaterialPackage100Service busMaterialPackage100Service;
    @Autowired
    private BusMaterialService busMaterialService;

    @Override
    public ResultBody validate(CriteriaSave cs, EntityMap requestMap) {
        if (FlymeUtils.isEmpty(requestMap.get("levelNo"))) {
            return ResultBody.failed("层级号不能为空");
        }else{
            String levelNo= requestMap.get("levelNo");
            if(levelNo.length()<11){
                return ResultBody.failed("层级号长度不正确");
            }
        }
        if (!requestMap.get("levelNo").toString().substring(0, 5).equals(requestMap.get("packageNo").toString())) {
            return ResultBody.failed("层级号与工程包代码不一致");
        }
        return ResultBody.ok();
    }
    @Override
    public void prepare(CriteriaSave criteriaSave, EntityMap entityMap, BusMaterial busMaterial) {
        BusProject project = busProjectService.getById(busMaterial.getProjectId());
        BusVersion planversion=busVersionService.getLatestVersion(project.getProjectId(),1);
        BusVersion packversion=busVersionService.getLatestVersion(project.getProjectId(),2);
        Long materialId = IdWorker.getId();
        BusMaterialPackage100 busPackage100 = new BusMaterialPackage100();
        BusMaterialPlan100 busPlan100 = new BusMaterialPlan100();
        Long  packersionId=IdWorker.getId();
        Long  planversionId=IdWorker.getId();
        if (FlymeUtils.isNotEmpty(entityMap.get("serviceCode"))) {
            BusVersion version=new BusVersion();
            if(FlymeUtils.isNotEmpty(packversion)){
                packersionId=packversion.getVersionId();
                BeanUtils.copyProperties(packversion,version);
            }else{
                project.setVersionpackageId(packersionId);
                project.setUserId(OpenHelper.getUserId());
                project.setVersion100(0);
                version.setVersionId(packersionId);
                version.setBigVersion("100");
                version.setSmallVerion("00");
                version.setUserId(OpenHelper.getUserId());
                version.setProjectId(project.getProjectId());
                version.setVersionType(2);
                version.setState(1);
            }
            busMaterial.setParentId(0L);
            busMaterial.setBeforeMarkQuantity(BigDecimal.valueOf(Double.parseDouble(entityMap.get("beforeMarkQuantity").toString())));
            busMaterial.setMaterialId(materialId);
            busMaterial.setProjectId(project.getProjectId());
            busMaterial.setConversionUnit(entityMap.get("conversionUnit"));
            busMaterial.setContractBillQuantity(BigDecimal.valueOf(Double.parseDouble(entityMap.get("contractBillQuantity").toString())));
            busMaterial.setContractSyntheticalPrice1(entityMap.getBigDecimal("contractMainPrice1").add(entityMap.getBigDecimal("installPrice1")));//合计  综合单价
            busMaterial.setContractSyntheticalPrice2(entityMap.getBigDecimal("contractMainPrice2").add(entityMap.getBigDecimal("installPrice2")));//合计 拟合同综合单价
            busMaterial.setContractSyntheticalPrice3((entityMap.getBigDecimal("contractMainPrice3").add(entityMap.getBigDecimal("installPrice13"))));//合计 其他综合单价
            busMaterial.setMainAmount(BigDecimal.ZERO);
            busMaterial.setMainAmount1(BigDecimal.ZERO);
            busMaterial.setMainAmount2(BigDecimal.ZERO);
            busMaterial.setMainAmount3(BigDecimal.ZERO);
            busMaterial.setInstallPrice(entityMap.getBigDecimal("installPrice"));
            busMaterial.setInstallPrice1(entityMap.getBigDecimal("installPrice1"));
            busMaterial.setInstallPrice2(entityMap.getBigDecimal("installPrice2"));
            busMaterial.setInstallPrice3(entityMap.getBigDecimal("installPrice3"));
            busMaterial.setSyntheticalPrice(entityMap.getBigDecimal("installPrice"));
            busMaterial.setSyntheticalPrice1(entityMap.getBigDecimal("installPrice1"));
            busMaterial.setSyntheticalPrice2(entityMap.getBigDecimal("installPrice2"));
            busMaterial.setSyntheticalPrice3(entityMap.getBigDecimal("installPrice3"));
            busMaterial.setSyntheticalAmount(entityMap.getBigDecimal("installPrice").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setSyntheticalAmount1(entityMap.getBigDecimal("installPrice1").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setSyntheticalAmount2(entityMap.getBigDecimal("installPrice2").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setSyntheticalAmount3(entityMap.getBigDecimal("installPrice3").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setInstallsumPrice1(entityMap.getBigDecimal("installPrice1").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setInstallsumPrice2(entityMap.getBigDecimal("installPrice2").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setInstallsumPrice3(entityMap.getBigDecimal("installPrice3").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setInstallAmount(entityMap.getBigDecimal("installPrice").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setInstallAmount1(entityMap.getBigDecimal("installPrice1").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setInstallAmount2(entityMap.getBigDecimal("installPrice2").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setInstallAmount3(entityMap.getBigDecimal("installPrice3").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setContractsumSyntheticalPrice1(entityMap.getBigDecimal("installPrice1").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setContractsumSyntheticalPrice2(entityMap.getBigDecimal("installPrice2").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setContractsumSyntheticalPrice3(entityMap.getBigDecimal("installPrice2").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setLoss(BigDecimal.valueOf(Double.parseDouble(entityMap.get("loss").toString())));
            busMaterial.setBeforeMarkQuantity(entityMap.getBigDecimal("beforeMarkQuantity"));
            busMaterial.setContractBillQuantity(entityMap.getBigDecimal("contractBillQuantity"));
            busMaterial.setMeasurementUnit(entityMap.get("measurementUnit"));
            busMaterial.setPlanPurchaseCount(entityMap.getBigDecimal("costQuantity100").multiply(entityMap.getBigDecimal("loss")));
            busMaterial.setConvertPurchaseCount(entityMap.getBigDecimal("costQuantity100").multiply(entityMap.getBigDecimal("loss")));
            busMaterial.setMainPrice(BigDecimal.ZERO);//成本主材费
            busMaterial.setMainPrice1(BigDecimal.ZERO);
            busMaterial.setMainPrice2(BigDecimal.ZERO);
            busMaterial.setMainPrice3(BigDecimal.ZERO);
            busMaterial.setCurrentMonthmainPrice(BigDecimal.ZERO);
            busMaterial.setCurrentMonthinstallPrice(BigDecimal.ZERO);
            busMaterial.setCurrentMonthsumPrice(BigDecimal.ZERO);
            busMaterial.setLastMonthinstallPrice(BigDecimal.ZERO);
            busMaterial.setLastMonthmainPrice(BigDecimal.ZERO);
            busMaterial.setLastMonthsumPrice(BigDecimal.ZERO);
            busMaterial.setTotalsumPrice(BigDecimal.ZERO);
            busMaterial.setTotalMonthinstallPrice(BigDecimal.ZERO);
            busMaterial.setTotalMonthmainPrice(BigDecimal.ZERO);
            busPackage100.setVersionStatus(1);
            busPackage100.setParentId(0L);
            busPackage100.setMaterialPackage100Id(materialId);//分包特殊处理
            busPackage100.setProjectId(project.getProjectId());
            busPackage100.setVersionId(packersionId);
            busPackage100.setLevelNo(entityMap.get("levelNo"));
            busPackage100.setMaterialId(materialId);
            busPackage100.setMajorName(entityMap.get("majorName"));
            busPackage100.setMajorCode(entityMap.get("majorCode"));
            busPackage100.setPackageName(entityMap.get("packageName"));
            busPackage100.setPackageNo(entityMap.get("packageNo"));
            busPackage100.setServiceCode(entityMap.get("serviceCode"));
            busPackage100.setServiceWbsCode(entityMap.get("serviceWbsCode"));
            busPackage100.setSubjectName(entityMap.get("subjectName"));
            busPackage100.setPackageDesc(entityMap.get("packageDesc"));
            busPackage100.setContractDesc(entityMap.get("contractDesc"));
            busPackage100.setMaterialArea(entityMap.get("materialArea"));
            busPackage100.setMaterialUse(entityMap.get("materialUse"));
            busPackage100.setCostQuantity100(entityMap.getBigDecimal("costQuantity100"));
            busPackage100.setCostQuantity200(entityMap.getBigDecimal("costQuantity200"));
            busPackage100.setPackageDesc(entityMap.get("packageDesc"));
            busPackage100.setStatus(0);//初始化导入状态 0 未转100 1转为100
            boolean a = busMaterialPackage100Service.save(busPackage100);
            busVersionService.saveOrUpdate(version);
            busProjectService.saveOrUpdate(project);
        }
        if(FlymeUtils.isNotEmpty(entityMap.get("materialCode"))){
            BusVersion version=new BusVersion();
            if(FlymeUtils.isNotEmpty(planversion)){
                planversionId=planversion.getVersionId();
                BeanUtils.copyProperties(planversion,version);
            }else{
                project.setVersionId(planversionId);
                project.setUserId(OpenHelper.getUserId());
                project.setVersion100(0);
                version.setBigVersion("100");
                version.setSmallVerion("00");
                version.setVersionId(planversionId);
                version.setUserId(OpenHelper.getUserId());
                version.setProjectId(project.getProjectId());
                version.setVersionType(1);
                version.setState(1);
            }
            //层级号 判断逻辑处理
            if (FlymeUtils.isNotEmpty(entityMap.get("levelNo"))) {
                String levelNo = entityMap.get("levelNo");
                busPlan100.setLevelNo(levelNo);
                Map<String, Object> busplanMap = Maps.newHashMap();
                busplanMap.put("levelNo", levelNo);
                List<BusMaterialPlan100> plan100List = (List<BusMaterialPlan100>) busMaterialPlan100Service.listByMap(busplanMap);
                if (FlymeUtils.isNotEmpty(plan100List)) {
                    busMaterial.setParentId(plan100List.get(0).getMaterialId());
                }
            }
            busMaterial.setParentId(0L);
            busMaterial.setBeforeMarkQuantity(BigDecimal.valueOf(Double.parseDouble(entityMap.get("beforeMarkQuantity").toString())));
            busMaterial.setMaterialId(materialId);
            busMaterial.setProjectId(project.getProjectId());
            busMaterial.setConversionUnit(entityMap.get("conversionUnit"));
            busMaterial.setContractBillQuantity(BigDecimal.valueOf(Double.parseDouble(entityMap.get("contractBillQuantity").toString())));
            busMaterial.setContractSyntheticalPrice1(entityMap.getBigDecimal("contractMainPrice1").add(entityMap.getBigDecimal("installPrice1")));//合计  综合单价
            busMaterial.setContractSyntheticalPrice2(entityMap.getBigDecimal("contractMainPrice2").add(entityMap.getBigDecimal("installPrice2")));//合计 拟合同综合单价
            busMaterial.setContractSyntheticalPrice3((entityMap.getBigDecimal("contractMainPrice3").add(entityMap.getBigDecimal("installPrice13"))));//合计 其他综合单价
            busMaterial.setMainAmount(entityMap.getBigDecimal("mainPrice").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setMainAmount1(entityMap.getBigDecimal("mainPrice1").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setMainAmount2(entityMap.getBigDecimal("mainPrice2").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setMainAmount3(entityMap.getBigDecimal("mainPrice3").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setMainsumPrice1(entityMap.getBigDecimal("mainPrice1").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setMainsumPrice2(entityMap.getBigDecimal("mainPrice2").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setMainsumPrice3(entityMap.getBigDecimal("mainPrice3").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setInstallPrice(BigDecimal.ZERO);
            busMaterial.setInstallPrice1(BigDecimal.ZERO);
            busMaterial.setInstallPrice2(BigDecimal.ZERO);
            busMaterial.setInstallPrice3(BigDecimal.ZERO);
            busMaterial.setMainPrice(entityMap.getBigDecimal("mainPrice"));
            busMaterial.setMainPrice1(entityMap.getBigDecimal("mainPrice1"));
            busMaterial.setMainPrice2(entityMap.getBigDecimal("mainPrice2"));
            busMaterial.setMainPrice3(entityMap.getBigDecimal("mainPrice3"));
            busMaterial.setSyntheticalPrice(entityMap.getBigDecimal("mainPrice"));
            busMaterial.setSyntheticalPrice1(entityMap.getBigDecimal("mainPrice1"));
            busMaterial.setSyntheticalPrice2(entityMap.getBigDecimal("mainPrice2"));
            busMaterial.setSyntheticalPrice3(entityMap.getBigDecimal("mainPrice3"));
            busMaterial.setInstallsumPrice1(BigDecimal.ZERO);
            busMaterial.setInstallsumPrice2(BigDecimal.ZERO);
            busMaterial.setInstallsumPrice3(BigDecimal.ZERO);
            busMaterial.setInstallAmount(BigDecimal.ZERO);
            busMaterial.setInstallAmount1(BigDecimal.ZERO);
            busMaterial.setInstallAmount2(BigDecimal.ZERO);
            busMaterial.setInstallAmount3(BigDecimal.ZERO);
            busMaterial.setContractsumSyntheticalPrice1(entityMap.getBigDecimal("mainPrice1").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setContractsumSyntheticalPrice2(entityMap.getBigDecimal("mainPrice2").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setContractsumSyntheticalPrice3(entityMap.getBigDecimal("mainPrice3").multiply(entityMap.getBigDecimal("costQuantity100")));
            busMaterial.setLoss(BigDecimal.valueOf(Double.parseDouble(entityMap.get("loss").toString())));
            busMaterial.setBeforeMarkQuantity(entityMap.getBigDecimal("beforeMarkQuantity"));
            busMaterial.setContractBillQuantity(entityMap.getBigDecimal("contractBillQuantity"));
            busMaterial.setMeasurementUnit(entityMap.get("measurementUnit"));
            busMaterial.setPlanPurchaseCount(entityMap.getBigDecimal("costQuantity100").multiply(entityMap.getBigDecimal("loss")));
            busMaterial.setConvertPurchaseCount(entityMap.getBigDecimal("costQuantity100").multiply(entityMap.getBigDecimal("loss")));
            busMaterial.setMainPrice(BigDecimal.ZERO);//成本主材费
            busMaterial.setMainPrice1(BigDecimal.ZERO);
            busMaterial.setMainPrice2(BigDecimal.ZERO);
            busMaterial.setMainPrice3(BigDecimal.ZERO);
            busMaterial.setCurrentMonthmainPrice(BigDecimal.ZERO);
            busMaterial.setCurrentMonthinstallPrice(BigDecimal.ZERO);
            busMaterial.setCurrentMonthsumPrice(BigDecimal.ZERO);
            busMaterial.setLastMonthinstallPrice(BigDecimal.ZERO);
            busMaterial.setLastMonthmainPrice(BigDecimal.ZERO);
            busMaterial.setLastMonthsumPrice(BigDecimal.ZERO);
            busMaterial.setTotalsumPrice(BigDecimal.ZERO);
            busMaterial.setTotalMonthinstallPrice(BigDecimal.ZERO);
            busMaterial.setTotalMonthmainPrice(BigDecimal.ZERO);
            busPlan100.setMaterialPlanId(IdWorker.getId());
            busPackage100.setVersionStatus(1);
            busPackage100.setParentId(0L);
            busPlan100.setMaterialId(materialId);
            busPlan100.setProjectId(project.getProjectId());
            busPlan100.setVersionId(planversionId);
            busPlan100.setMajorName(entityMap.get("majorName"));
            busPlan100.setMajorCode(entityMap.get("majorCode"));
            busPlan100.setPackageName(entityMap.get("packageName"));
            busPlan100.setPackageNo(entityMap.get("packageNo"));
            busPlan100.setMaterialWbsCode(entityMap.get("materialWbsCode"));
//            busPlan100.setMaterialName(entityMap.get("materialName"));
//            busPlan100.setMaterialCode(entityMap.get("materialCode"));
            String materialCode=entityMap.get("materialCode");
            if(FlymeUtils.isNotEmpty(materialCode)){
                String[] strs=materialCode.split(" ");
                busPlan100.setMaterialCode(strs[0]);
                busPlan100.setMaterialName(strs[1]);
            }
            busPlan100.setSubjectName(entityMap.get("subjectName"));
            busPlan100.setContractDesc(entityMap.get("contractDesc"));
            busPlan100.setMaterialArea(entityMap.get("materialArea"));
            busPlan100.setMaterialUse(entityMap.get("materialUse"));
            busPlan100.setLevelNo(entityMap.get("levelNo"));
            busPlan100.setCostQuantity100(entityMap.getBigDecimal("costQuantity100"));
            busPlan100.setPlanPurchaseCount(entityMap.getBigDecimal("costQuantity100").multiply(entityMap.getBigDecimal("loss")));
            busPlan100.setEndPurchaseCount(entityMap.getBigDecimal("costQuantity100").multiply(entityMap.getBigDecimal("loss")));
            busPlan100.setZtPurchaseCount(BigDecimal.ZERO);
            busPlan100.setDhPurchaseCount(BigDecimal.ZERO);
            busPlan100.setCostQuantity200(BigDecimal.ZERO);
            busPlan100.setMeasurementUnit(entityMap.get("measurementUnit"));
            busPlan100.setBrandName(entityMap.get("brandName"));
            busPlan100.setRemark(entityMap.get("remark"));
            busPlan100.setLoss(entityMap.getBigDecimal("loss"));
            busPlan100.setTechnicalData(entityMap.get("technicalData"));
            busPlan100.setContractDesc(entityMap.get("contractDesc"));
            busPlan100.setMateriaSpecs(entityMap.get("materiaSpecs"));
            busPlan100.setVersionStatus(1);
            busPlan100.setOrderflg("0");
            busPlan100.setApplayStatus(0);
            boolean b = busMaterialPlan100Service.save(busPlan100);
            busVersionService.saveOrUpdate(version);
            busProjectService.saveOrUpdate(project);
            }
      }
 }



